package com.example.netty.bean;

import com.example.netty.exception.BadFormatException;
import com.example.netty.exception.ErrorMessageException;
import com.example.netty.util.DateUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * @Author : lichong
 * @description :
 * @Date Create in 19:49 2020/1/11
 * @Modified By : 设备信息
 *      *[4位信息长度][6位消息类型][12位设备码][TIMEyyyyMMddHmmss][body]#
 *      *0040DF0001A00000000001TIME2020011120000012.5#
 **/
public class DeviceMsg {
    /**
     * 消息体的开始标识
     */
    public static final String MSG_START = "*";
    /**
     * 消息体的结束标识
     */
    public static final String MSG_END = "#";
    /**
     * 消息的长度
     */
    public static final int MSG_LENGTH = 4;
    /**
     * 消息类型长度
     */
    public static final int TYPE_LENGTH = 6;
    /**
     * 设备码长度
     */
    public static final int SNNO_LENGTH = 12;
    /**
     * 时间长度
     */
    public static final int TIME_LENGTH = 14;

    /**
     * 最小的消息体长度
     *      *0040DF0001A00000000001TIME2020011120000012.5#
     */
    public static final int MESSAGE_MIN_LENGTH = MSG_START.length() + MSG_END.length() + MSG_LENGTH + TYPE_LENGTH + SNNO_LENGTH + TIME_LENGTH + "TIME".length();

    /**
     * 未除去前后标识的信息体
     */
    private String msg;

    /**
     *除去前后标识和信息长度的信息体
     */
    private String msgBody;

    public DeviceMsg() {
    }

    public DeviceMsg(String msg) {
        this.msg = msg;
        getInfo();
    }

    /**
     * 设备码
     */
    private String snNo;
    /**
     * 消息类型
     */
    private String msgType;
    /**
     * 上传的时间
     */
    private Date time;
    /**
     * 数据信息，多个信息用英文逗号分隔
     */
    private String data;

    /**
     * 获取信息体
     * @return
     */
    public String getBody() {
        if (StringUtils.isBlank(msg)) {
            throw new ErrorMessageException("设备上传信息为空");
        }
        if (!StringUtils.startsWith(msg, MSG_START) ||
                !StringUtils.endsWith(msg, MSG_END)) {
            throw new BadFormatException("错误的格式信息");
        }
        //获取去除前头后尾的信息
        String msg1 = msg.substring(1, msg.length() - 1);
        int length = Integer.parseInt(msg1.substring(0, MSG_LENGTH));
        String body = msg1.substring(MSG_LENGTH);
        if (body.length() == length) {
            return body;
        } else {
            throw new BadFormatException("信息长度不合法");
        }
    }

    /**
     * 解析信息体
     */
    private void getInfo() {
        this.msgBody = getBody();
        try {
            int index = 0;
            //获取设备编码
            msgType = msgBody.substring(index, index = index + TYPE_LENGTH);
            //获取序列号
            snNo = msgBody.substring(index, index = index + SNNO_LENGTH);
            //获取时间串
            String timeStr = msgBody.substring(index + 4, index = index + 4 + TIME_LENGTH);
            time = DateUtils.parseString2Date(timeStr, "yyyyMMddHHmmss");
            data = msgBody.substring(index);
        } catch (Exception e) {
            throw new BadFormatException("数据转换异常");
        }
    }

    public String getSnNo() {
        return snNo;
    }

    public String getMsgType() {
        return msgType;
    }

    public Date getTime() {
        return time;
    }

    public String getData() {
        return data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
        this.getInfo();
    }

    public String getMsgBody() {
        return msgBody;
    }

}
